---
title: "Untitled"
author: "melissa_barales"
date: "4/23/2022"
output: pdf_document
---


```{r}
#setting working directory
setwd("~/Desktop/Senior Thesis/Data/Pooled CES")
```

```{r}
library(tidyverse)
library(haven)
library(stargazer)
```

```{r}
#uploading pooled CES dataset
dat <- read_dta(file = "cumulative_2006-2021.dta")
```

```{r}
#subsetting the data to years 2012-2020
dat2 <-subset(dat, dat$year==2016 | dat$year==2017 | dat$year==2018 | 
                dat$year==2019 | dat$year==2020)
```

```{r}
table(dat2$year)
```

```{r}
#checking for NAs in zipcode
table(is.na(dat2$zipcode))
```

**RACE/ETHNICITY**
```{r}
#summarizing race/ethnicity 
table(dat2$race)
```

```{r}
#1: White
#2: Black/AA
#3: Hispanic/Latino
#4: Asian-American 
#5: Native American
#6: Mixed Race
#7: Other
#8: Middle Eastern
```

```{r}
prop.table(table(dat2$race))
```

```{r}
#identify as Hispanic
table(dat2$hispanic)
```

```{r}
#1: yes
#2: no
```

```{r}
#Creating a new race variable to combine self-identified hispanic respondents 
dat2$race_new <- ifelse(dat2$hispanic == 1 | dat2$race==3, 3, dat2$race)
```

```{r}
table(dat2$race_new)
```


```{r}
#Merging in 2020 dataset
dat3 <- read_dta(file = "CES20_Common_OUTPUT_vv.dta")
```

*Immigration status*
```{r}
table(dat3$immstat)
```

```{r}
#1 - immigrant and naturalized citizen
#2 - immigrant and citizen
#3 - US-born but w/ imm. parents
#4 - US-born but w/ imm. grandparents
#5 - US-born but w/ no imm. heritage
```

*Political activity in social media*
```{r}
#Recoding certain variables 
```

```{r}
#Recent social media use - Posted a story, photo, video or 
#link about politics
dat3$sm_post <- dat3$CC20_300d_1
```

```{r}
#Recent social media use - Posted a comment 
#about politics
dat3$sm_comment <- dat3$CC20_300d_2
```

```{r}
#Recent social media use - Read a story or watched a 
#video about politics
dat3$sm_story <- dat3$CC20_300d_3
```

```{r}
#Recent social media use - Followed a political event
dat3$sm_event <- dat3$CC20_300d_4
```

```{r}
#Recent social media use - Forwarded a story, 
#photo, video or link about politics to friends
dat3$sm_forward <- dat3$CC20_300d_5
```

```{r}
#presidential primary 
dat3$pres_primary <- dat3$CL_2020ppvm
```

```{r}
table(dat3$pres_primary)
```
```{r}
class(dat3$pres_primary)
```
```{r}
dat3$pres_primary <- as.numeric(dat3$pres_primary)
```

```{r}
class(dat3$pres_primary)
```

*Political acitivity outcomes*
```{r}
#Renaming political activity variables
```

```{r}
#Past year - Attend local political meetings
dat3$political_meeting <- dat3$CC20_430a_1
```

```{r}
#Past year - Put up a political sign
dat3$political_sign <- dat3$CC20_430a_2
```

```{r}
#Past year - Work for a candidate or campaign
dat3$campaign_volunteer <- dat3$CC20_430a_3
```

```{r}
table(dat3$CC20_430a_3)
```

```{r}
#Past year - Attend a political protest, march or demonstration
dat3$political_protest <- dat3$CC20_430a_4
```

```{r}
#Past year - Contact a public official
dat3$political_contact <- dat3$CC20_430a_5
```

```{r}
#Past year - Donate money to a candidate, campaign, or political organization
dat3$political_donation <- dat3$CC20_430a_6
```

```{r}
#Political attitudes - Congress
#Do you approve of the way each is doing their job...
dat3$att_congress <- dat3$CC20_320b
```

```{r}
#Political attitudes - Supreme Court 
#Do you approve of the way each is doing their job...
dat3$att_supreme <- dat3$CC20_320c
```

**Combining 2020 CES w/ pooled data**
```{r}
CES20_trim <- dat3 %>% select(caseid, immstat,
                              sm_post, 
                              sm_comment, sm_story, sm_event, sm_forward, 
                              political_meeting, political_sign, 
                              campaign_volunteer, political_protest,
                              political_contact, 
                              political_donation, 
                              att_congress, att_supreme,
                              pres_primary, CC20_361) %>% rename (case_id = 
                                                                caseid) %>% mutate(case_id=as.character(case_id))
```

**Combining 2019 CES w/ pooled data**

```{r}
CES_2019 <- read_dta(file = "CCES19_Common_OUTPUT.dta")
```

```{r}
table(CES_2019$immstat)
```

```{r}
#Attend a political protest
CES_2019$political_protest <- CES_2019$CC19_303_12
```

```{r}
#Contacted a public official
CES_2019$political_contact <- CES_2019$CC19_303_13
```

```{r}
#City_residence 
CES_2019$city_residence <- CES_2019$citylength_1
```

```{r}
#Recoding city_residence variable 
CES_2019$city_residence[CES_2019$city_residence<=10] <- 1
CES_2019$city_residence[CES_2019$city_residence>10] <- 2
table(CES_2019$city_residence)
```

*Social media activity*
```{r}
#Posted a story, photo, video or link about politics
CES_2019$sm_post <- CES_2019$CC19_300d_1
```

```{r}
#Posted a comment about politics
CES_2019$sm_comment <- CES_2019$CC19_300d_2
```

```{r}
#Read a story or watched a video about politics
CES_2019$sm_story <- CES_2019$CC19_300d_3
```

```{r}
#Followed a political event
CES_2019$sm_event <- CES_2019$CC19_300d_4
```

```{r}
#Forwarded a story, photo, video or link about politics to friends
CES_2019$sm_forward <- CES_2019$CC19_300d_5
```

```{r}
#Political attitudes - Supreme Court 
#Do you approve of the way each is doing their job...
CES_2019$att_supreme <- CES_2019$CC19_308d
```

```{r}
CES19_trim <- CES_2019 %>% select(caseid, immstat, 
                                  political_protest,
                                  political_contact,
                                  sm_post, sm_comment, sm_story, sm_event, 
                                  sm_forward, att_supreme, city_residence, citylength_1) %>% rename (case_id =  caseid) %>% mutate(case_id=as.character(case_id))
```

**Combining 2018 CES w/ pooled data**

```{r}
CES_2018 <- read_dta(file = "cces18_common_vv.dta")
```

```{r}
table(CES_2018$immstat)
```

*Political activity* 

```{r}
#Attend a political meeting 
CES_2018$political_meeting <- CES_2018$CC18_417a_1
```

```{r}
#Put up a political sign
CES_2018$political_sign <- CES_2018$CC18_417a_2
```

```{r}
#Work for a candidate or campaign
CES_2018$campaign_volunteer <- CES_2018$CC18_417a_3
```

```{r}
#Attend a political protest, march or demonstration
CES_2018$political_protest <- CES_2018$CC18_417a_4
```

```{r}
#Contacted a public official
CES_2018$political_contact <- CES_2018$CC18_417a_5
```

```{r}
#Donate money to a candiate/campaign
CES_2018$political_donation <- CES_2018$CC18_417a_6
```

*Social media activity*
```{r}
#Posted a story, photo, video or link about politics
CES_2018$sm_post <- CES_2018$CC18_300d_1
```

```{r}
#Posted a comment about politics
CES_2018$sm_comment <- CES_2018$CC18_300d_2
```

```{r}
#Read a story or watched a video about politics
CES_2018$sm_story <- CES_2018$CC18_300d_3
```

```{r}
#Followed a political event
CES_2018$sm_event <- CES_2018$CC18_300d_4
```

```{r}
#Forwarded a story, photo, video or link about politics to friends
CES_2018$sm_forward <- CES_2018$CC18_300d_5
```

```{r}
#Political attitudes - Congress
#Do you approve of the way each is doing their job...
CES_2018$att_congress <- CES_2018$CC18_308b
```

```{r}
#Political attitudes - Supreme Court 
#Do you approve of the way each is doing their job...
CES_2018$att_supreme <- CES_2018$CC18_308c
```

```{r}
#City_residence 
CES_2018$city_residence <- CES_2018$citylength_1
```

```{r}
#Recoding city residence variable
CES_2018$city_residence[CES_2018$city_residence<=10] <- 1
CES_2018$city_residence[CES_2018$city_residence>10] <- 2
table(CES_2018$city_residence)
``` 

```{r}
CES18_trim <- CES_2018 %>% select(caseid, immstat, 
                                  political_protest, political_donation,
                                  political_contact, political_meeting,
                                  political_sign, 
                                  campaign_volunteer,
                                  sm_post, sm_comment, sm_story, sm_event, 
                                  sm_forward, att_congress, att_supreme, city_residence, citylength_1) %>% rename (case_id =  caseid) %>% mutate(case_id=as.character(case_id))
```

**Combining 2017 CES w/ pooled data**

```{r}
CES_2017 <- read_dta(file = "CCES17 Data.dta")
```

```{r}
table(CES_2017$immstat)
```

```{r}
#Political attitudes - Supreme Court 
#Do you approve of the way each is doing their job...
CES_2017$att_supreme <- CES_2017$CC17_322d
```

```{r}
#Attend a political protest
CES_2017$political_protest <- CES_2017$CC17_304_12
```

```{r}
#Contacted a public official
CES_2017$political_contact <- CES_2017$CC17_304_13
```

```{r}
#City_residence 
CES_2017$city_residence <- CES_2017$citylength_1
```

```{r}
CES_2017$city_residence[CES_2017$city_residence<=10] <- 1
CES_2017$city_residence[CES_2017$city_residence>10] <- 2
table(CES_2017$city_residence)
```

```{r}
CES17_trim <- CES_2017 %>% select(V101, immstat, att_supreme, 
                                  political_protest, political_contact, city_residence,
                                  citylength_1) %>% rename (case_id =  V101) %>% mutate(case_id=as.character(case_id))
```

**Combining 2016 CES w/ pooled data**

```{r}
CES_2016 <- read_dta(file = "CCES16_Common_OUTPUT_Feb2018_VV.dta")
```

```{r}
table(CES_2016$immstat)
```

*Political activity*

```{r}
#Attend local political meetings
CES_2016$political_meeting <- CES_2016$CC16_417a_1
```

```{r}
#Put up a political sign

CES_2016$political_sign <- CES_2016$CC16_417a_2
```

```{r}
#Work for a candidate or campaign

CES_2016$campaign_volunteer <- CES_2016$CC16_417a_3
```

```{r}
#Donate to a campaign/candidate

CES_2016$political_donation <- CES_2016$CC16_417a_4
```

*Social Media*

```{r}
#Posted a story, photo, video or link about politics
CES_2016$sm_post <- CES_2016$CC16_300d_1
```

```{r}
#Posted a comment about politics
CES_2016$sm_comment <- CES_2016$CC16_300d_2
```

```{r}
#Read a story or watched a video about politics
CES_2016$sm_story <- CES_2016$CC16_300d_3
```

```{r}
#Followed a political event
CES_2016$sm_event <- CES_2016$CC16_300d_4
```

```{r}
#Forwarded a story, photo, video or link about politics to friends
CES_2016$sm_forward <- CES_2016$CC16_300d_5
```

```{r}
#Political attitudes - Congress
#Do you approve of the way each is doing their job...
CES_2016$att_congress <- CES_2016$CC16_320b
```

```{r}
#Political attitudes - Supreme Court 
#Do you approve of the way each is doing their job...
CES_2016$att_supreme <- CES_2016$CC16_320c
```


```{r}
#City_residence 
CES_2016$city_residence <- CES_2016$citylength_1
```

```{r}
CES_2016$city_residence[CES_2016$city_residence<=10] <- 1
CES_2016$city_residence[CES_2016$city_residence>10] <- 2
table(CES_2016$city_residence)
```

```{r}
CES16_trim <- CES_2016 %>% select(V101, immstat, 
                                  political_donation,
                                  political_meeting, political_sign, 
                                  campaign_volunteer,
                                  sm_post, sm_comment, sm_story, sm_event, 
                                  sm_forward, att_supreme, att_congress, city_residence, 
                                  citylength_1) %>% rename (case_id =  V101) %>% mutate(case_id=as.character(case_id))
```

```{r}
#COMBINING ALL SURVEYS TOGETHER
combine_survey <- bind_rows(CES20_trim, CES19_trim, CES18_trim, CES17_trim,
                            CES16_trim)
```

```{r}
#MERGING IT IN WITH MASTER DATASET
Pooled_dat <- dat2 %>% left_join(combine_survey, by="case_id")
```


```{r}
#Making a new dataset
Pooled_dat_2 <- Pooled_dat
```

```{r}
table(Pooled_dat_2$year)
```

```{r}
#Immigrants categorized as respondents 1, 2, andd 3
table(Pooled_dat_2$immstat)
```

```{r}
#Making immigration into a binary variable
Pooled_dat_2$imm_status <- NA
Pooled_dat_2$imm_status[Pooled_dat_2$immstat <= 3] <- 1
Pooled_dat_2$imm_status[Pooled_dat_2$immstat > 3] <- 2
```

```{r}
table(Pooled_dat_2$imm_status)
```

```{r}
#Creating new independent variable for turnout that excludes 2018 data
dat_one <- subset(dat2, dat2$year!=2018)
```

```{r}
table(dat_one$vv_turnout_gvm, dat_one$year)
```

```{r}
dat_one$vv_turnout_gvm2 <- NA
dat_one$vv_turnout_gvm2 <- dat_one$vv_turnout_gvm
```

```{r}
table(dat_one$vv_turnout_gvm2, dat_one$year)
```

```{r}
dat_one <- dat_one %>% select(case_id, vv_turnout_gvm2)
```

```{r}
#Merging this variable into master dataset 
Pooled_dat_2 <- Pooled_dat_2 %>% left_join(dat_one, by="case_id")
```

```{r}
# Exported the final dataset
saveRDS(Pooled_dat_2, "~/Desktop/Senior Thesis/Data/Pooled CES/Pooled Data.rds")
```


